package com.cs.bd.utils;

import android.text.TextUtils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {
    private static final String LOG_TAG = "appcenter_file";

    public static void mkDir(String dir) {
        File file = new File(dir);
        if (!file.exists()) {
            try {
                file.mkdirs();
            } catch (Exception localException) {
            }
        }
        file = null;
    }

    public static File createNewFile(String path, boolean append) {
        File newFile = new File(path);
        if (!append) {
            if (newFile.exists()) {
                newFile.delete();
            } else {
                File prePngFile = new File(path + ".png");
                if ((prePngFile != null) && (prePngFile.exists())) {
                    prePngFile.delete();
                }
            }
        }
        if (!newFile.exists()) {
            try {
                File parent = newFile.getParentFile();
                if ((parent != null) && (!parent.exists())) {
                    parent.mkdirs();
                }
                newFile.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return newFile;
    }

    public static boolean createFile(String destFileName, boolean replace) {
        File file = new File(destFileName);
        if (file.exists()) {
            if (replace) {
                file.delete();
            } else {
                return false;
            }
        }
        if (destFileName.endsWith(File.separator)) {
            return false;
        }
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                return false;
            }
        }
        try {
            if (file.createNewFile()) {
                return true;
            }
            return false;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean saveStringToSDFile(String string, String fileName) {
        if (TextUtils.isEmpty(string)) {
            return false;
        }
        try {
            return saveByteToSDFile(string.getBytes("UTF-8"), fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean saveByteToSDFile(byte[] byteData, String filePathName) {
        if ((byteData == null) || (TextUtils.isEmpty(filePathName))) {
            return false;
        }
        boolean result = false;
        try {
            File newFile = createNewFile(filePathName, false);
            FileOutputStream fileOutputStream = new FileOutputStream(newFile);
            fileOutputStream.write(byteData);
            fileOutputStream.flush();
            fileOutputStream.close();
            result = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean saveInputStreamToSDFile(InputStream inputStream, String filePathName) {
        OutputStream os = null;
        try {
            File file = createNewFile(filePathName, false);
            os = new FileOutputStream(file);
            byte[] buffer = new byte['?'];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            os.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static void copyFile(String srcStr, String decStr) {
        File srcFile = new File(srcStr);
        if (!srcFile.exists()) {
            return;
        }
        File decFile = new File(decStr);
        if (!decFile.exists()) {
            File parent = decFile.getParentFile();
            parent.mkdirs();
            try {
                decFile.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
        InputStream input = null;
        OutputStream output = null;
        try {
            input = new FileInputStream(srcFile);
            output = new FileOutputStream(decFile);
            byte[] data = new byte['?'];
            for (; ; ) {
                int len = input.read(data);
                if (len <= 0) {
                    break;
                }
                output.write(data);
            }
            return;
        } catch (Exception localException3) {
        } finally {
            if (null != input) {
                try {
                    input.close();
                } catch (Exception localException6) {
                }
            }
            if (null != output) {
                try {
                    output.close();
                } catch (Exception localException7) {
                }
            }
        }
    }

    public static byte[] readByteFromSDFile(String filePathName) {
        byte[] bs = null;
        try {
            File newFile = new File(filePathName);
            FileInputStream fileInputStream = new FileInputStream(newFile);
            DataInputStream dataInputStream = new DataInputStream(fileInputStream);
            BufferedInputStream inPutStream = new BufferedInputStream(dataInputStream);
            bs = new byte[(int) newFile.length()];
            inPutStream.read(bs);
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bs;
    }

    public static String readFileToString(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return null;
        }
        File file = new File(filePath);
        if (!file.exists()) {
            return null;
        }
        try {
            InputStream inputStream = new FileInputStream(file);
            return readInputStream(inputStream, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String readInputStream(InputStream in, String charset)
            throws IOException {
        if (in == null) {
            return "";
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int bufferLength = 1024;
        try {
            byte[] buf = new byte['?'];
            int len = 0;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            byte[] data = out.toByteArray();
            return new String(data, TextUtils.isEmpty(charset) ? "UTF-8" : charset);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
        return null;
    }

    public static String readInputStreamWithLength(InputStream in, String charset, int length)
            throws IOException {
        if (in == null) {
            return "";
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int bufferLength = 1024;
        try {
            byte[] buf = new byte['?'];
            int len = 0;
            int i = 0;
            while (((len = in.read(buf)) > 0) && (i < length)) {
                out.write(buf, 0, len);
                i++;
            }
            byte[] data = out.toByteArray();
            return new String(data, TextUtils.isEmpty(charset) ? "UTF-8" : charset);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
        return null;
    }

    public static boolean deleteDirectory(String sPath) {
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }
        File dirFile = new File(sPath);
        if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
            return false;
        }
        boolean flag = true;

        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                flag = deleteFile(files[i].getAbsolutePath());
                if (!flag) {
                    break;
                }
            } else {
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag) {
                    break;
                }
            }
        }
        if (!flag) {
            return false;
        }
        if (dirFile.delete()) {
            return true;
        }
        return false;
    }

    public static boolean deleteFile(String filePath) {
        boolean result = false;
        if (!TextUtils.isEmpty(filePath)) {
            File file = new File(filePath);
            if (file.exists()) {
                result = file.delete();
            }
        }
        return result;
    }

    public static boolean isFileExist(String filePath) {
        boolean result = false;
        try {
            File file = new File(filePath);
            result = file.exists();
            file = null;
        } catch (Exception localException) {
        }
        return result;
    }

    public static long getFileSize(String path) {
        long size = 0L;
        if (path != null) {
            File file = new File(path);
            size = file.length();
        }
        return size;
    }
}
